<%-- 
    Document   : process
    Created on : Apr 7, 2011, 10:25:35 AM
    Author     : Muhammad Anis
--%>
<%@page import="system.chatbetweenfriend"%>
<%@page import="tools.commandProcessor"%>
<%@page import="user.user"%>
<%@page import="tools.dbconnector"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="tools.writer" %>
<jsp:useBean id="chatbeetwenfriend" class="system.chatbetweenfriend" scope="application" />
<jsp:useBean id="allover" class="system.allover" scope="application" />
<%

            String task = request.getParameter("task");
            if (task != null) {
                dbconnector db = new dbconnector(out);
                if (task.equals("register")) {
                    //register r                   
                    String nama = request.getParameter("name_reg");
                    String username = request.getParameter("username_reg");
                    String password_reg = request.getParameter("password_reg");
                    String email = request.getParameter("email_reg");
                    db.register(nama, username, password_reg, email);
                    writer.redirect("../index.jsp?task=register", out);
                } else if (task.equals("login")) {
                    String nama = request.getParameter("username");
                    String password = request.getParameter("password");
                    if (db.login(nama, password)) {
                        user currentuser = new user(nama);
                        session.setAttribute("currentuser", currentuser);
						session.setAttribute("username", nama);
						if (nama.equals("admin")){
                            writer.redirect("../admin.jsp", out);
                        } else {
							writer.redirect("../chat.jsp", out);
						}
                    } else {
                        writer.redirect("../index.jsp?task=auth", out);
                    }
				} else if (task.equals("logout")){
                    String username = (String) session.getAttribute("username");
                    db.logout(username);
                    writer.redirect("../index.jsp", out);
                } else if (task.equals("processCommand")) {

                    user currentuser = (user) session.getAttribute("currentuser");
                    String tipe = request.getParameter("tipe");
                    if (tipe.equals("command")) {
                        //lagi memberikan command
                        String command = request.getParameter("command");
                        try {
                            commandProcessor cp = new commandProcessor(command);
                            if (cp.command.equals("/addfriend")) {
                                if(cp.arg1.trim()==""){
                                    //do nothing
                                }else{
                                    currentuser.addFriends(cp.arg1.trim());
                                    currentuser.printFriends(out);
                                    session.setAttribute("currentuser", currentuser);
                                }
                            } else if(cp.command.equals("/create")){
                                String namacategory = request.getParameter("category");
                                if(!cp.arg2.trim().equals("")){
                                    namacategory = cp.arg2.trim();
                                }
                                if(!cp.arg1.trim().equals("")){
                                    //masukkan ke dalam room
                                    allover.addRoom(cp.arg1.trim(),currentuser.username , namacategory);
                                }
                            }
                        } catch (Exception e) {
                            writer.write(e.getMessage(), out);
                        }
                    }
                    else{
                        //lagi chatting
                        //cek apakah dia chatting dengan teman atau dalam room
                        String tipeChat = request.getParameter("tipechat");
                        //cek nama room atau friend
                        String nama = request.getParameter("nama");
                        String command = request.getParameter("command");
                        if(tipeChat.equals("friend")){
                            //masukkan ke dalam class chatbeetwenfriend
                            chatbeetwenfriend.addChat(currentuser.username, nama, command);
                            chatbeetwenfriend.printChat(currentuser.username, nama, out);
                        }
                        else if(tipeChat.equals("room")){
                            //masukkan ke dalam room                            
                            allover.addChatinRoom(currentuser.username, nama, command);
                        }
                        
                    }
                } else if (task.equals("updateFriends")) {
                    user currentuser = (user) session.getAttribute("currentuser");
                    currentuser.printFriends(out);
                } else if(task.equals("deletefriend")){
                    user currentuser = (user) session.getAttribute("currentuser");
                    currentuser.deleteFriend();
                    session.setAttribute("currentuser", currentuser);
                } else if (task.equals("updateuser")) {
                    String username = request.getParameter("uname");
                    String max = request.getParameter("maxroom");
                    String ban = request.getParameter("ban_option");
                    int maxRoom = Integer.parseInt(max);
                    int isBan = 0;
                    if (ban.equals("ban")){
                        isBan = 1;
                    }
                    db.updateUser(maxRoom, username, isBan);
                    writer.redirect("../admin.jsp", out);
                } else if (task.equals("changekategori")){
                    String teks_id = request.getParameter("uname");
                    String nama = request.getParameter("namakat");
                    String mod = request.getParameter("mod_option");
                    String parent = request.getParameter("parent");
                    int id = Integer.parseInt(teks_id);
                    db.updateKategori(id, nama, mod);
                    db.updateSub(nama, parent);
                    writer.redirect("../admin.jsp", out);
                } else if (task.equals("dropkategori")){
                    String teks_id = request.getParameter("uname");
                    int id = Integer.parseInt(teks_id);
                    db.dropKategori(id);
                    writer.redirect("../admin.jsp", out);
                } else if (task.equals("addkategori")){
                    String nama = request.getParameter("kat_baru");
                    String parent = request.getParameter("kat_baru_option");
                    String mod = request.getParameter("mod_baru_option");
                    if (!nama.isEmpty()){
                        if (parent.equals("null")){
                            db.addKategori(nama, mod);
                        } else {
                            db.addKategori(nama, parent, mod);
                        }
                    }
                    writer.redirect("../admin.jsp", out);
                }
            } else {
                writer.redirect("../index.jsp", out);
            }
%>